// pages/lucky_discount/lucky_discount.js
var common = require("../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        showInfoTip: false,
        //授权失败的弹框
        warrant: true,
        order_id: "",
        regular: [],
        code: -1,
        info: "",
        max: "",
        status: "",
        luck_img: "",
        discountBg: "",
        isShow: false
    },
    getUserProfile() {
        var that = this;
        common.getUserInfoNew({
            success(res) {
                that.setData({
                    isShow: true
                });
                that.getData();
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            order_id: options.order_id || "",
            s_uid: options.uid || ""
        });
        this.getInfo();
        this.discountOpen();
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        let that = this;
        var order_id = that.data.order_id;
        let max = that.data.max;
        return {
            title: "拼手气，第" + max + "个人领取的红包最大！",
            path: "/pages/lucky_discount/lucky_discount?order_id=" + order_id,
            imageUrl: that.data.luck_img
        };
    },
    getData: function() {
        var that = this;
        var order_id = that.data.order_id;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Voucher/luckyvou/getvoucher",
            method: "post",
            data: {
                order_id: order_id,
                openid: openid
            },
            success: res => {
                if (res.extra.content != "") {
                    var regular = res.extra.content.split("；");
                }
                that.setData({
                    regular: regular,
                    code: res.extra.code,
                    info: res.extra,
                    max: parseInt(res.extra.max) - 1
                });
                if (res.extra.code == 5) {
                    wx.showToast({
                        title: "该红包你已经领过啦！",
                        icon: "none"
                    });
                }
            },
            fail: res => {
                wx.showToast({
                    title: res.message,
                    icon: "none"
                });
            }
        });
    },
    continueGetSet: function() {
        let that = this;
        wx.getSetting({
            success(res) {}
        });
        wx.openSetting({
            success(res) {
                if (res.authSetting["scope.userInfo"]) {
                    that.getUserInfo();
                    that.setData({
                        warrant: true
                    });
                }
                if (that.data.warrant && res.authSetting["scope.userInfo"]) {
                    that.setData({
                        showInfoTip: false
                    });
                }
            }
        });
    },
    getUserInfo: function() {
        common.getUserInfo();
        wx.redirectTo({
            url: "/pages/lucky_discount/lucky_discount?order_id=" + this.data.order_id
        });
    },
    discountOpen: function() {
        var that = this;
        common.request({
            endpoint: "Voucher/luckyvou/isoff",
            method: "post",
            success: res => {
                that.setData({
                    status: res.extra.status
                });
                if (res.extra.status == 0) {
                    wx.showModal({
                        title: "提示",
                        content: "拼手气红包已被关闭！",
                        success: res => {
                            wx.redirectTo({
                                url: "/pages/index/index"
                            });
                        }
                    });
                }
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main,
                    level_set: res.extra.level_set
                });
                that.setData({
                    luck_img: res.extra.luck_img
                });
                if (that.data.s_uid) {
                    that.levelShare();
                }
            }
        });
    },
    /**
   * 分销商绑定上下级
   */
    levelShare: function() {
        let that = this;
        common.request({
            endpoint: "Level/level/level-share",
            method: "post",
            data: {
                user_id: that.data.s_uid
            },
            success: res => {
                if (that.data.level_set.success_bind_tip) {
                    wx.showToast({
                        icon: "none",
                        title: that.data.level_set.success_bind_tip
                    });
                } else {
                    wx.showToast({
                        icon: "none",
                        title: "绑定成功"
                    });
                }
            },
            fail(err) {}
        });
    },
    getInfo: function() {
        let that = this;
        common.request({
            endpoint: "Voucher/luckyvou/getvoucher_bg",
            method: "post",
            data: {},
            success: res => {
                that.setData({
                    discountBg: res.extra
                });
            }
        });
    }
});